package com.app.controller.push;

import com.app.model.main.*;
import com.app.model.push.*;
import com.app.model.response.RetCode;
import com.app.model.response.RetStatus;
import com.app.model.util.ArticleFlag;
import com.app.repository.interfaces.*;
import com.app.service.ResponseService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/4/12. It's a beautiful day.
 */
@RequestMapping(value = "/push")
@RestController
public class PushController {

    @Autowired
    private ResponseService responseService;
    @Autowired
    private DeviceTokenMapper deviceTokenMapper;
    @Autowired
    private NotifyMapper notifyMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private CommentsOfArticleMapper commentsOfArticleMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private CommentsOfCommentsMapper commentsOfCommentsMapper;
    @Autowired
    private ApprovalMapper approvalMapper;
    @Autowired
    private SystemMessageMapper systemMessageMapper;
    @Autowired
    private AccusationMapper accusationMapper;
    @Autowired
    private FavoriteItemMapper favoriteItemMapper;

    @RequestMapping(value = "/setDeviceToken")
    public RetStatus setDeviceToken(@RequestBody String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int id = (int)paraMap.get("userId");
        long userId = (long)id;
        String deviceTokenContent = (String)paraMap.get("deviceToken");

        if(userId < 1 || userId > userMapper.getMaxId()){
            return responseService.doResponse(218);
        }

        DeviceToken deviceToken = new DeviceToken();
        deviceToken.setUserId(userId)
                .setDeviceTokenContent(deviceTokenContent);

        DeviceToken token = deviceTokenMapper.selectByUserId(userId);
        if(token == null){
            if(deviceTokenMapper.insert(deviceToken) == 1){
                return responseService.doResponse(100);
            }
        }else {
            deviceToken.setId(token.getId());
            if(deviceTokenMapper.updateByPrimaryKey(deviceToken) == 1){
                return responseService.doResponse(100);
            }
        }

        return responseService.doResponse(200);
    }

    @RequestMapping(value = "/setRead")
    public RetStatus setRead(@RequestBody String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int id = (int)paraMap.get("id");
        long notificationId = (long)id;
        String time = (String)paraMap.get("time");

        if(notificationId < 1 || notificationId > notifyMapper.getMaxId()){
            return responseService.doResponse(218);
        }

        Notify notify = notifyMapper.selectByPrimaryKey(notificationId);
        notify.setIsRead((byte)1)
                .setTime(time);

        if (notifyMapper.updateByPrimaryKeySelective(notify) == 1){
            return responseService.doResponse(100);
        }else {
            return responseService.doResponse(200);
        }
    }

    @RequestMapping(value = "/getNotification")
    public RetStatus getNotification(@RequestBody String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int id = (int)paraMap.get("userId");
        long userId = (long)id;

        List<BaseResponseBody> resultList = new ArrayList<>();

        List<Notify> notifyList = notifyMapper.selectByReceiverId(userId);

        for (Notify notify : notifyList){
            byte type = notify.getType();

            switch (type){
                case NotificationType.COMMENT_HELPARTICLE:
                case NotificationType.COMMENT_SHAREARTICLE:
                    UserInfo userInfo1 = userInfoMapper.selectByPrimaryKey(notify.getSenderId());
                    CommentsOfArticle commentsOfArticle1 = commentsOfArticleMapper.selectByPrimaryKey(notify.getObjectId());
                    Article article1 = articleMapper.selectByPrimaryKey(commentsOfArticle1.getArticleId());

                    ResponseBody1 responseBody1 = new ResponseBody1();
                    responseBody1.setNotificationId(notify.getId())
                            .setUserId(userInfo1.getId())
                            .setAvator(imageMapper.selectByPrimaryKey(userInfo1.getAvator()).getOrigin())
                            .setNickName(userInfo1.getNickName())
                            .setArticleId(article1.getId())
                            .setTitle(article1.getTitle())
                            .setComment1Id(commentsOfArticle1.getId())
                            .setContent(commentsOfArticle1.getContent())
                            .setTime(commentsOfArticle1.getTime());

                    BaseResponseBody baseResponseBody = new BaseResponseBody();
                    baseResponseBody.setObject(responseBody1)
                            .setType(article1.getType() == ArticleFlag.HELP ? NotificationType.COMMENT_HELPARTICLE : NotificationType.COMMENT_SHAREARTICLE);

                    resultList.add(baseResponseBody);
                    break;
                case NotificationType.COMMENT_COMMENTS:
                    UserInfo userInfo3 = userInfoMapper.selectByPrimaryKey(notify.getSenderId());
                    CommentsOfComments commentsOfComments3 = commentsOfCommentsMapper.selectByPrimaryKey(notify.getObjectId());
                    CommentsOfArticle commentsOfArticle3 = commentsOfArticleMapper.selectByPrimaryKey(commentsOfComments3.getCommentsId());

                    ResponseBody3 responseBody3 = new ResponseBody3();
                    responseBody3.setNotificationId(notify.getId())
                            .setUserId(userInfo3.getId())
                            .setAvator(imageMapper.selectByPrimaryKey(userInfo3.getAvator()).getOrigin())
                            .setNickName(userInfo3.getNickName())
                            .setComment1Id(commentsOfArticle3.getId())
                            .setComment2Id(commentsOfComments3.getId())
                            .setContent(commentsOfComments3.getContent())
                            .setTime(commentsOfComments3.getTime());

                    BaseResponseBody baseResponseBody3 = new BaseResponseBody();
                    baseResponseBody3.setObject(responseBody3)
                            .setType(NotificationType.COMMENT_COMMENTS);

                    resultList.add(baseResponseBody3);
                    break;
                case NotificationType.APPROVAL_HELPARTICLE:
                case NotificationType.APPROVAL_SHAREARTICLE:
                    UserInfo userInfo4 = userInfoMapper.selectByPrimaryKey(notify.getSenderId());
                    Approval approval4 = approvalMapper.selectByPrimaryKey(notify.getObjectId());
                    Article article4 = articleMapper.selectByPrimaryKey(approval4.getObjectId());

                    ResponseBody4 responseBody4 = new ResponseBody4();
                    responseBody4.setNotificationId(notify.getId())
                            .setUserId(userInfo4.getId())
                            .setAvator(imageMapper.selectByPrimaryKey(userInfo4.getAvator()).getOrigin())
                            .setNickName(userInfo4.getNickName())
                            .setArticleId(article4.getId())
                            .setTitle(article4.getTitle())
                            .setTime(approval4.getTime());

                    BaseResponseBody baseResponseBody4 = new BaseResponseBody();
                    baseResponseBody4.setObject(responseBody4)
                            .setType(approval4.getType() == ArticleFlag.HELP ? NotificationType.APPROVAL_HELPARTICLE : NotificationType.APPROVAL_SHAREARTICLE);


                    resultList.add(baseResponseBody4);
                    break;
                case NotificationType.APPROVAL_COMMENTS:
                    UserInfo userInfo6 = userInfoMapper.selectByPrimaryKey(notify.getSenderId());
                    Approval approval6 = approvalMapper.selectByPrimaryKey(notify.getObjectId());
                    CommentsOfArticle commentsOfArticle6 = commentsOfArticleMapper.selectByPrimaryKey(approval6.getObjectId());

                    ResponseBody6 responseBody6 = new ResponseBody6();
                    responseBody6.setNotificationId(notify.getId())
                            .setUserId(userInfo6.getId())
                            .setAvator(imageMapper.selectByPrimaryKey(userInfo6.getAvator()).getOrigin())
                            .setNickName(userInfo6.getNickName())
                            .setComment1Id(commentsOfArticle6.getId())
                            .setTime(approval6.getTime());

                    BaseResponseBody baseResponseBody6 = new BaseResponseBody();
                    baseResponseBody6.setObject(responseBody6)
                            .setType(NotificationType.APPROVAL_COMMENTS);

                    resultList.add(baseResponseBody6);
                    break;
                case NotificationType.APPROVAL_USER:
                    UserInfo userInfo7 = userInfoMapper.selectByPrimaryKey(notify.getSenderId());
                    Approval approval7 = approvalMapper.selectByPrimaryKey(notify.getObjectId());

                    ResponseBody7 responseBody7 = new ResponseBody7();
                    responseBody7.setNotificationId(notify.getId())
                            .setUserId(userInfo7.getId())
                            .setAvator(imageMapper.selectByPrimaryKey(userInfo7.getAvator()).getOrigin())
                            .setNickName(userInfo7.getNickName())
                            .setMainId(notify.getReceiverId())
                            .setTime(approval7.getTime());

                    BaseResponseBody baseResponseBody7 = new BaseResponseBody();
                    baseResponseBody7.setObject(responseBody7)
                            .setType(NotificationType.APPROVAL_USER);

                    resultList.add(baseResponseBody7);
                    break;
                case NotificationType.FAVORITE_HELPARTICLE:
                case NotificationType.FAVORITE_SHAREARTICLE:
                    UserInfo userInfo8 = userInfoMapper.selectByPrimaryKey(notify.getSenderId());
                    FavoriteItem favoriteItem8 = favoriteItemMapper.selectByPrimaryKey(notify.getObjectId());
                    Article article8 = articleMapper.selectByPrimaryKey(favoriteItem8.getObjectId());

                    ResponseBody8 responseBody8 = new ResponseBody8();
                    responseBody8.setNotificationId(notify.getId())
                            .setUserId(userInfo8.getId())
                            .setAvator(imageMapper.selectByPrimaryKey(userInfo8.getAvator()).getOrigin())
                            .setNickName(userInfo8.getNickName())
                            .setArticleId(article8.getId())
                            .setTitle(article8.getTitle())
                            .setTime(favoriteItem8.getTime());

                    BaseResponseBody baseResponseBody8 = new BaseResponseBody();
                    baseResponseBody8.setObject(responseBody8)
                            .setType(article8.getType() == ArticleFlag.HELP ? NotificationType.FAVORITE_HELPARTICLE : NotificationType.FAVORITE_SHAREARTICLE);

                    resultList.add(baseResponseBody8);
                    break;
                case NotificationType.FAVORITE_COMMENTS:
                    UserInfo userInfo10 = userInfoMapper.selectByPrimaryKey(notify.getSenderId());
                    FavoriteItem favoriteItem10 = favoriteItemMapper.selectByPrimaryKey(notify.getObjectId());
                    CommentsOfArticle commentsOfArticle10 = commentsOfArticleMapper.selectByPrimaryKey(favoriteItem10.getObjectId());

                    ResponseBody10 responseBody10 = new ResponseBody10();
                    responseBody10.setNotificationId(notify.getId())
                            .setUserId(userInfo10.getId())
                            .setAvator(imageMapper.selectByPrimaryKey(userInfo10.getAvator()).getOrigin())
                            .setNickName(userInfo10.getNickName())
                            .setComment1Id(commentsOfArticle10.getId())
                            .setTime(favoriteItem10.getTime());

                    BaseResponseBody baseResponseBody10 = new BaseResponseBody();
                    baseResponseBody10.setObject(responseBody10).setType(NotificationType.FAVORITE_COMMENTS);

                    resultList.add(baseResponseBody10);
                    break;
                case NotificationType.SYSTEM_MESSAGE:
                    SystemMessage systemMessage11 = systemMessageMapper.selectByPrimaryKey(notify.getObjectId());

                    ResponseBody11 responseBody11 = new ResponseBody11();
                    responseBody11.setNotificationId(notify.getId())
                            .setContent(systemMessage11.getMessage())
                            .setTime(systemMessage11.getCreateTime());

                    BaseResponseBody baseResponseBody11 = new BaseResponseBody();
                    baseResponseBody11.setObject(responseBody11)
                            .setType(NotificationType.SYSTEM_MESSAGE);

                    resultList.add(baseResponseBody11);
                    break;
                case NotificationType.ACCUSATION_NOTIFICATION:
                    Accusation accusation12 = accusationMapper.selectByPrimaryKey(notify.getObjectId());
                    if(accusation12 == null){
                        break;
                    }

                    ResponseBody12 responseBody12 = new ResponseBody12();
                    responseBody12.setNotificationId(notify.getId())
                            .setObjectId(accusation12.getObjectId())
                            .setFlag(accusation12.getType())
                            .setReason(accusation12.getReason())
                            .setTime(accusation12.getTime());

                    BaseResponseBody baseResponseBody12 = new BaseResponseBody();
                    baseResponseBody12.setObject(responseBody12)
                            .setType(NotificationType.ACCUSATION_NOTIFICATION);

                    resultList.add(baseResponseBody12);
                    break;
                case NotificationType.ACCUSATION_HANDLE_RESULT:
                    break;
                case NotificationType.ACCUSATION_RECEIVED_REPLY:
                    ResponseBody14 responseBody14 = new ResponseBody14();
                    responseBody14.setNotificationId(notify.getId())
                            .setContent("谢谢您的参与，对于您的举报我们一定会谨慎处理。")
                            .setTime(Long.toString(System.currentTimeMillis()));

                    BaseResponseBody baseResponseBody14 = new BaseResponseBody();
                    baseResponseBody14.setObject(responseBody14)
                            .setType(NotificationType.ACCUSATION_RECEIVED_REPLY);

                    resultList.add(baseResponseBody14);
                    break;
                default:break;
            }
        }

        RetStatus retStatus = new RetStatus();
        retStatus.setCode(100)
                .setMessage(RetCode.getMessage(100))
                .setBody(resultList);

        return retStatus;
    }

}
